package com.fanqwz.hexieshe.test.databinding;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.fanqwz.hexieshe.R;
import com.fanqwz.hexieshe.data.API.wp.WPInterface;
import com.fanqwz.hexieshe.data.entity.wp.Comment;
import com.fanqwz.hexieshe.databinding.ActivityMainBinding;

import java.util.List;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private User user = new User("haha", 12);
    Interceptor interceptor = chain -> {
        okhttp3.Response r = chain.proceed(chain.request());
        return r.newBuilder().build();
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.setUser(user);

        OkHttpClient client = new OkHttpClient().newBuilder()
                .addNetworkInterceptor(interceptor)
                .build();
        Retrofit retrofit = new Retrofit.Builder(

        )
                .baseUrl("https://www.hexieshe.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        WPInterface wp = retrofit.create(WPInterface.class);
        Call<List<Comment>> p = wp.getComment(1000);
        p.enqueue(new Callback<List<Comment>>() {
            @Override
            public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {
                MainActivity.this.runOnUiThread(()->{
                    user.setName(response.body().toString());
                });
            }

            @Override
            public void onFailure(Call<List<Comment>> call, Throwable t) {
                user.setName(t.getMessage());
            }
        });
    }

    public void onClick(View v) {
        user.setName("hehe"+ user.getName());
        Toast.makeText(this, user.getName(), Toast.LENGTH_LONG).show();
    }

    private boolean isThereInternetConnection() {
        boolean isConnected;

        ConnectivityManager connectivityManager =
                (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        isConnected = (networkInfo != null && networkInfo.isConnectedOrConnecting());

        return isConnected;
    }
}
//    // Used to load the 'native-lib' library on application startup.
//    static {
//        System.loadLibrary("native-lib");
//    }
//
//    /**
//     * A native method that is implemented by the 'native-lib' native library,
//     * which is packaged with this application.
//     */
//    public native String stringFromJNI();
//}
